package com.lv.service.room.company

import android.content.Context
import android.os.Parcelable
import androidx.annotation.Keep
import androidx.annotation.Nullable
import androidx.lifecycle.LiveData
import androidx.room.*
import com.lv.service.bean.*
import com.lv.service.room.helper.*
import kotlinx.parcelize.Parcelize

/**
 * 作者： ygx
 * 创建日期：2022/1/8 2:42 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * App的公共业务基础数据类
 */

//3、database
@Database(entities = [CompanyItemBean::class], version = 1, exportSchema = false)
abstract class CompanyDatabase : RoomDatabase() {

    abstract val companyDao: CompanyDao

    companion object {

        private const val USER_DB_NAME = "syy_o_company_db_7"

        @Volatile
        private var instance: CompanyDatabase? = null

        @Synchronized
        fun getInstance(context: Context): CompanyDatabase {
            return instance ?: Room.databaseBuilder(
                context,
                CompanyDatabase::class.java,
                USER_DB_NAME
            ).build().also { instance = it }
        }
    }
}


//1、entity的定义
@TypeConverters(
    InspectionStringConverter::class,
    InspectionCompnayContactConverter::class,
    InspectionCompnayCertificationConverter::class,
    InspectionCompnayDisplayFacConverter::class,
    InspectionCompnayCategoryMoreConverter::class,
    InspectionCompnayDisplayProConverter::class,
    InspectionCompnayBperiodsBeanConverter::class,
    InspectionConditionConverter::class,
    InspectionEwmsConverter::class,
    InspectionAddressDetailConverter::class,
)
@Parcelize
@Entity(tableName = "company_db_7")
@Keep
data class CompanyItemBean(
    @PrimaryKey
    val _id: Int,//主键
    var id: String,
    val name: String?,
    @Nullable
    val logo: String?,
    @Nullable
    var address: String?,
    @Nullable
    var province: String?,
    @Nullable
    val created: Long?,
    @Nullable
    val fcaid: String?,
    @Nullable
    val caption: String?,
    @Nullable
    val attentionnum: Int?,
    @Nullable
    var phone: String?,//登录号码
    @Nullable
    var companytel: String?,//打电话的号码
    @Nullable
    var bnames: String?,
    @Nullable
    val newtemplate: String?,
    @Nullable
    val access: Int?,
    @Nullable
    val callnum: Int?,
    @Nullable
    val publishnum: Int?,
//    var display: List<DisplayFacBean>?,
    @Nullable
    var display: String?,
    @Nullable
    var city: String?,
    @Nullable
    var book: String?,
    @Nullable
    val longitude: String?,
    @Nullable
    val latitude: String?,
    @Nullable
    var islisted: String?,//0 下线 1 上线 2 审核中
    @Nullable
    val secaid: String?,
    @Nullable
    var area: String?,
    @Nullable
    var prefecture: String?,
    @Nullable
    var isbind: String?,
    @Nullable
    val txaccid: String?,
    @Nullable
    val isaccess: String?,
    @Nullable
    val isexpire: String?,//0免费版 1付费版
    val periods: String,
    @Nullable
    val openid: String?,
    @Nullable
    val identification: String?,
    @Nullable
    val fcname: String?,
    @Nullable
    val secname: String?,
    @Nullable
    val introduction: String?,
    @Nullable
    val status: String?,
//    val whVersion: String,
    @Nullable
    val contact: List<ContactBean>?,
    @Nullable
    var certification: List<CertificationBean>?,
    @Nullable
    val displayFac: List<DisplayFacBean>?,
    @Nullable
    val category: CategoryMoreBean?,
    @Nullable
    val displayPro: DisplayPro?,
    @Nullable
    val bperiods: BperiodsBean?,
    @Nullable
    var isbindTel: String?,
    @Nullable
    var spuListssize: Int?,
    @Nullable
    var totalintroduction: String?,
    @Nullable
    var fintroduction: String?,
    @Nullable
    var watermarking: String?,
    @Nullable
    var problem: List<String>?,
    var condition: ConditionBean?,
    var remgoodssum: Int?,
    var hotgoodssum: Int?,
    val ewms: List<EwmsBean>?,
    var wxappid:String?,
    var wxghid:String?,
    var site:List<AddressDetailsBean>?,
//    val spuLists: List<GoodsItemBean>?,

//    val thumbupList: List<String>,
//    val collectionList: List<String>,
) : Parcelable
////1、entity的定义
//
//@Entity(tableName = "tb_cniao_user")
//data class CniaoUserInfo(
//    @PrimaryKey
//    val id: Int,//主键
//    val course_permission: Boolean,
//    val token: String?,
//    @Embedded
//    val user: User?
//) {
//    @Keep
//    data class User(
//        @ColumnInfo(name = "cniao_user_id")
//        val id: Int,//用户id
//        val logo_url: String?,//用户头像
//        val reg_time: String?,//用户注册时间
//        val username: String?//用户名
//    )
//}

//2、dao层的定义
@Dao
interface CompanyDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertFindCompany(companyItemBean: CompanyItemBean)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertFindSuspendCompany(companyItemBean: CompanyItemBean)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertFindSuspendCompany(companyItems: List<CompanyItemBean>)

    @Update(onConflict = OnConflictStrategy.REPLACE)
    fun updateFindCompany(companyItemBean: CompanyItemBean)

    @Delete
    fun deleteFindCompany(companyItemBean: CompanyItemBean)

    @Query("delete from company_db_7")
    fun deleteAllCompany()

    @Query("delete from company_db_7")
    suspend fun deleteSuspendAllCompany()

    // = , in , like
    @Query("select * from company_db_7 where _id=:id")
    fun queryLiveFindCompany(id: Int = 0): LiveData<CompanyItemBean>

    @Query("select * from company_db_7")
    fun queryLiveFindAllCompany(): LiveData<List<CompanyItemBean>>

//    @Query("select * from category_db where _id=:id")
//    fun queryFindCategory(id: Int = 0): CategoryBean?

//    @Query("select * from category_db")
//    fun queryFindAllCategory(): List<CategoryBean>?

    @Query("select * from company_db_7 where _id =:id")
    suspend fun getFindCompany(id: Int = 0): CompanyItemBean?

    @Query("select * from company_db_7")
    suspend fun getAllCompany(): List<CompanyItemBean>?
}



